/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bpmlab.shift.bean;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 *
 * @author Mikael Lima
 */
public class JsfUtil {

    public static final String USUARIO_LOGADO = "User";
    
    public static void colocarNaSessao(String key, Object object) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.getSessionMap().put(key, object);
    }

    public static void retirarDaSessao(String key) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.getSessionMap().remove(key);
    }

    public static Object obterDaSessao(String key) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        return externalContext.getSessionMap().get(key);
    }
    
    public static boolean estarNaSessao(String key) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        return externalContext.getSessionMap().containsKey(key);
    }
    
    public static String obterUsuarioRemoto() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        return externalContext.getRemoteUser();
    }
    
    public static void mensagem(Severity severity, String summary, String detail) {
        FacesMessage fm = new FacesMessage(severity, summary, detail);
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.addMessage(null, fm);
    }
 
    public static void mensagemSalvoComSucesso() {
        mensagem(FacesMessage.SEVERITY_INFO, "Salvo com sucesso!", "");
    }
    
    public static void mensagemErroAoSalvar() {
        mensagem(FacesMessage.SEVERITY_ERROR, "Erro ao salvar!", "");
    }
    
    public static void mensagemRemovidoComSucesso() {
        mensagem(FacesMessage.SEVERITY_INFO, "Removido com sucesso!", "");
    }
    
    public static void mensagemErroAoRemover() {
        mensagem(FacesMessage.SEVERITY_ERROR, "Erro ao remover!", "");
    }
}
